package com.langsin.shixu;

import cn.hutool.core.util.IdUtil;
import com.langsin.bean.*;
import com.langsin.shixu.mapper.GoodsMapper;
import com.langsin.shixu.mapper.TeacherMapper;
import com.langsin.shixu.mapper.UserMapper;
import com.langsin.shixu.mapper.WorkerMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.nio.MappedByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ShixuText {

    SqlSession sqlSession = null;

    @Before  //每次测试之前执行 test方法执行之前
    public void textBefore() throws Exception {
        //读取mybatis配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        //创建(建立工厂)对象
        SqlSessionFactoryBuilder build = new SqlSessionFactoryBuilder();
        //创建stream工厂
        SqlSessionFactory factory = build.build(stream);
        //创建一个会话，默认不自动提交  使用会话查询结果
        sqlSession = factory.openSession();

    }


    //普通测试。
    @Test
    public void text1() throws Exception {
        //查询worker   wid = 5af9f088-5e5d-11e9-9dc0-28d244d68b7c
        Worker worker = sqlSession.selectOne("worker.selectWorker", "5af9f088-5e5d-11e9-9dc0-28d244d68b7c");
        //结果输出
        System.out.println(worker);

        //修改测试
        Worker worker1 = new Worker();
        worker1.setAge(500);
        worker1.setWid("5af9f088-5e5d-11e9-9dc0-28d244d68b7c");
        int update = sqlSession.update("worker.updateAgeById", worker1);
        System.out.println(update);

    }


    // Mapper测试 , 接口 ➕ mapper.xml文件 进行 动态代理 创建 实现类，类似于dao层
    @Test
    public void textMapperWorker() throws Exception {

        //获得mapper 接口 动态代理 的 实现类
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);

        //增加
        // 主键 整数自增, 字符串uuid , 雪花算法(课下自己了解)
        Worker worker = new Worker();
        worker.setWid(IdUtil.getSnowflakeNextIdStr());
        worker.setWname("焰灵姬");
        worker.setAge(30);
        worker.setSex("女");
        int i = workerMapper.insertWorker(worker);
        System.out.println(i);

        //查询
        List<Worker> workers = workerMapper.selectByAge(50);
        workers.forEach(System.out::println);

    }

    //数据库中 uuid 方法的测试
    @Test
    public void textMapperWorkerUUID() throws Exception {

        //获得mapper 接口 动态代理 的 实现类
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);

        //uuid方法测试 (增加)
        Worker worker = new Worker();
        worker.setWname("紫女");
        worker.setAge(25);
        worker.setSex("女");
        int i = workerMapper.insertWorkerUUID(worker);
        System.out.println(i);


    }


    // 模糊查询 测试 GoodsMapper 中的测试
    //#{value} 测试
    @Test
    public void textMapperGoodsLike() throws Exception {

        //获得mapper 接口 动态代理 的 实现类
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);

        String name = "%C%";
        List<Goods> goods = goodsMapper.queryByName(name);
        goods.forEach(System.out::println);
    }

    // 模糊查询 测试 GoodsMapper 中的测试
    //${value} 测试
    @Test
    public void textMapperGoodsLike2() throws Exception {

        //获得mapper 接口 动态代理 的 实现类
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);

        String name = "X";
        List<Goods> goods = goodsMapper.queryByName2(name);
        goods.forEach(System.out::println);


        String tableName1 = "worker";  //查询表名
        String tableName2 = "goods";  //查询表名
        int counts1 = goodsMapper.counts(tableName1);
        System.out.println("worker" + "=" + counts1);
        int counts2 = goodsMapper.counts(tableName2);
        System.out.println("goods" + "=" + counts2);

    }


    // 自动驼峰 映射查询 测试 UserMapper 中的测试
    @Test
    public void textMapperUserZiDong() throws Exception {

        //获得mapper 接口 动态代理 的 实现类
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        int uid = 702;
        User user = userMapper.queryById(uid);
        System.out.println(user);

    }

    // 手动驼峰 映射查询 (一些表名不采用驼峰命名法，无法坐到数据库中的列名与java类中的变量一致)
    // 测试 UserMapper 中的测试  使用Teacher表 测试
    @Test
    public void textMapperUserShouDong() throws Exception {

        //获得mapper 接口 动态代理 的 实现类
        TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);

        int tid = 1009;
        Teacher teacher = teacherMapper.queryById(tid);
        System.out.println(teacher);

    }


    // 入参（多个参数——map、对象等） 测试
    @Test
    public void textMapperUserRuCan() throws Exception {

        //获得mapper 接口 动态代理 的 实现类
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //map传多个参数
        HashMap<String, Object> map = new HashMap<>();
        map.put("roleCode", "plain");
        map.put("address", "济南浪曦");
        List<User> users = userMapper.queryUserByMap(map);
        users.forEach(System.out::println);

        //对象传多个参数
        QueryVo queryVo = new QueryVo();
        queryVo.setRoleCode("plain");
        queryVo.setAddress("济南浪曦");
        List<User> users1 = userMapper.queryByRoleAndAddress(queryVo);
        users1.forEach(System.out::println);

    }


    // 入参 是 List 测试
    @Test
    public void textMapperGoodsList() throws Exception {

        //获得mapper 接口 动态代理 的 实现类
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);

        List<Integer> listgoodsId = new ArrayList<>();
        listgoodsId.add(9003);
        listgoodsId.add(9007);
        listgoodsId.add(9008);
        List<Goods> goods = mapper.queryByIds(listgoodsId);
        goods.forEach(System.out::println);

        // 注解查询
        Goods goods1 = mapper.queryByIdAno(9003);
        System.out.println("goods1 = " + goods1);

    }


    // 多条件查询 （例如：根据不同的需求查询商品） 测试
    @Test
    public void textMapperGoods_OneAndMore() throws Exception {

        //获得mapper 接口 动态代理 的 实现类
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        Goods goods = new Goods();
        goods.setGoodsName("%老干妈%");
        goods.setShopPrice(13.0);
        goods.setGoodsStock(400);

        List<Goods> goods1 = mapper.queryByCondition(goods);
        goods1.forEach(System.out::println);

    }



    //按条件修改 数据库数据
    @Test
    public void textMapperWorker_Update() throws Exception {

        //获得mapper 接口 动态代理 的 实现类
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);

        Worker worker = new Worker();
        worker.setWid("1540706610441166848");
        worker.setWname("焰灵姬");
        worker.setAge(30);
        worker.setSex("女");
        int i = workerMapper.updateWorkerByCondition(worker);
        System.out.println(i);

    }


    @After  //每次测试之后执行 test方法执行之后
    public void textAfter() throws Exception {

        sqlSession.commit(); //提交(主要用于修改是提交数据库)
        sqlSession.close(); //关闭

    }

}
